﻿using Kenly.DBFramework.Gateway;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Data.Linq;

using Kenly.DBFramework.TestModel;
using Kenly.DBFramework.TestModel.Purchasing;

namespace Kenly.DBFramework.Tests
{
 
    /// <summary>
    ///这是 GatewayConfigFactoryTest 的测试类，旨在
    ///包含所有 GatewayConfigFactoryTest 单元测试
    ///</summary>
    [TestClass()]
    public class ConfigFactoryTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        // 
        //编写测试时，还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        ///FindGatewayTypes 的测试
        ///</summary>
        [TestMethod()]
        public void FindGatewayTypesTest()
        {
            IList<Type> actual = ConfigFactory.FindGatewayTypes(typeof(Vendor).Assembly);
            Assert.IsTrue(actual.Count > 0);
            Assert.IsTrue(actual.Contains(typeof(Vendor)));

            actual = ConfigFactory.FindGatewayTypes(typeof(Assert).Assembly);
            Assert.IsTrue(actual.Count == 0);
        }

        ///IsGatewayType 的测试
        ///</summary>
        [TestMethod()]
        public void IsGatewayTypeTest()
        {
            bool actual = ConfigFactory.IsGatewayType(typeof(Vendor));
            Assert.IsTrue(actual);

            actual = ConfigFactory.IsGatewayType(null);
            Assert.IsFalse(actual);

            actual = ConfigFactory.IsGatewayType(typeof(object));
            Assert.IsFalse(actual);
        }


        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void CreateConfigsTest()
        {
            GatewayConfig defaultConfig = new GatewayConfig
            {
                ConnectionString = SystemConfig.ConnectionString,
                DatabaseType = SystemConfig.DatabaseType
            };

            IDictionary<Type, GatewayConfig> configs = ConfigFactory.CreateConfigs(typeof(Vendor).Assembly, defaultConfig);

            Assert.IsTrue(configs.Count > 0);
            Assert.IsTrue(configs.ContainsKey(typeof(Vendor)));

            List<GatewayConfig> gatewayConfigs = new List<GatewayConfig>(configs.Values);

            Assert.IsTrue(gatewayConfigs.TrueForAll(p => p != defaultConfig));

            Assert.IsTrue(gatewayConfigs.TrueForAll(p => p.ConnectionString == defaultConfig.ConnectionString));
            Assert.IsTrue(gatewayConfigs.TrueForAll(p => p.DatabaseType == defaultConfig.DatabaseType));
            Assert.IsTrue(gatewayConfigs.TrueForAll(p => p.CommandTimeout == defaultConfig.CommandTimeout));
        }

        /// <summary>
        /// 
        /// </summary>
        [TestMethod()]
        public void SetupGatewayConfigTest()
        {
            ModelConfigFactory factory = new ModelConfigFactory();

            Assert.IsTrue(factory.Configs.Count > 0);
            Assert.IsTrue(factory.Configs[typeof(Vendor)].DatabaseType == DatabaseType.SqlServer);
        }
    }
}
